




clear

* First set the sample size
set obs 1000
gen id=_n

* Create a data structure
set seed 25591
sca n_respondents = 1000 /* same as above */
sca n_groups = 100
sca n_questions = 5 /* number of questions to build the scale */
sca n_responses = 5 /* number of ordered responses per questions in the Likert scale */


* Next assign 1000 participants to discussion groups of 10 each 
gen groupid=0
foreach i of numlist 1/100 {
foreach j of numlist 1/10 {
qui replace groupid = `i' if _n==`j'+10*(`i'-1)
}
}

* place roughly a third into each of three sites
gen site1 = 0
replace site1 = 1 if groupid< n_groups/3
gen site2 = 0
replace site2 = 1 if groupid < 2*n_groups/3 & site1==0
gen site3 = 0
replace site3 = 1 if site1==0 & site2==0


* Count the number of people in each group -- fixed at 10 in this example
bysort groupid: egen gcount=count(groupid)

* Assign pretreatment ideological ideal points
gen theta0=rnormal()
gen liberal = theta0<=(-0.5)
gen conservative = theta0>=(0.5)


* Take the average of all ideal points in the group other than the participant's own ideal point
bysort groupid: egen Htheta0 = mean(theta0)
replace Htheta0 = (Htheta0*gcount - theta0)/(gcount - 1)
* gen Htheta0sq = Htheta0^2
bysort groupid: egen Htheta0sq = mean(theta0^2)
replace Htheta0sq = (Htheta0sq*gcount - theta0^2)/(gcount - 1)

* Then create delta_theta
gen delta_theta = 1*Htheta0 + 1*Htheta0sq*liberal - 1*Htheta0sq*conservative + 0.3*rnormal()

 * Next, assign group-level means for post-treatement, within-group dependence for each question and assign delta_zetas
 foreach i of numlist 1/5 {
 gen zeta`i' = rnormal()
 bysort groupid: egen Hzeta`i' = mean(zeta`i')
 gen delta_zeta`i' = 3*Hzeta`i' + 1*rnormal()  /* multiplying Hzeta by 3 yields roughly a 1 SD change  */
  }
 
 * Create the IRT parameters and generate pretreatment responses
 sca lambda0 = 0.5
 sca lambda1 = 1
 foreach i of numlist 1/5 {
 gen preO`i' = .
 gen xpreO`i' = lambda0 + lambda1*theta0 + 1*rnormal()
 replace preO`i' = 1 if xpreO`i' <= 0
 replace preO`i' = 2 if xpreO`i' > 0 & xpreO`i' <= 1
 replace preO`i' = 3 if xpreO`i' > 1 & xpreO`i' <= 2
 replace preO`i' = 4 if xpreO`i' > 2 & xpreO`i' <= 3
 replace preO`i' = 5 if xpreO`i' > 3
   }
 
 * And do the same for posttreatmet responses
 sca beta0 = 0.5
 sca beta1 = 0.5
 sca beta2 = 1
 sca beta3 = 1
 sca beta4 = 0.5
 sca beta5 = -0.5
 sca beta6 = 0.5
 foreach i of numlist 1/5 {
 gen postO`i' = .
 gen xpostO`i' = beta0 + beta1*preO`i' + beta2*theta0 + beta3*delta_theta + beta4*delta_zeta`i' + beta5*site2 + beta6*site3 + 1*rnormal()
 replace postO`i' = 1 if xpostO`i' <= 0
 replace postO`i' = 2 if xpostO`i' > 0 & xpostO`i' <= 1
 replace postO`i' = 3 if xpostO`i' > 1 & xpostO`i' <= 2
 replace postO`i' = 4 if xpostO`i' > 2 & xpostO`i' <= 3
 replace postO`i' = 5 if xpostO`i' > 3
  }
 
polychoric preO1 preO2 preO3 preO4 preO5
polyfactor
predict f0x
egen f0 = std(f0x)
polychoric postO1 postO2 postO3 postO4 postO5
polyfactor
predict f1x
egen f1 = std(f1x)


bysort groupid: egen Hf0 = mean(f0)
replace Hf0 = (Hf0*10 - f0)/(10 - 1)
bysort groupid: egen Hf0sq = mean(f0^2)
replace Hf0sq = (Hf0sq*10 - f0^2)/(10 - 1)

* account for delta_zeta1 in the xt clustering
xtset groupid

gen libxHtheta0sq=liberal*Htheta0sq
gen conxHtheta0sq=conservative*Htheta0sq
gen libxHf0sq=liberal*Hf0sq
gen conxHf0sq=conservative*Hf0sq

* "true" continuous case used for the benchmark
xtreg xpostO1 xpreO1 theta0 Htheta0 libxHtheta0sq conxHtheta0sq site2 site3
xtreg xpostO2 xpreO2 theta0 Htheta0 libxHtheta0sq conxHtheta0sq site2 site3
xtreg xpostO3 xpreO3 theta0 Htheta0 libxHtheta0sq conxHtheta0sq site2 site3
xtreg xpostO4 xpreO4 theta0 Htheta0 libxHtheta0sq conxHtheta0sq site2 site3
xtreg xpostO5 xpreO5 theta0 Htheta0 libxHtheta0sq conxHtheta0sq site2 site3


* and for comparison, "measured" continuous case
xtreg xpostO1 xpreO1 f0 Hf0 libxHf0sq conxHf0sq site2 site3
xtreg xpostO2 xpreO2 f0 Hf0 libxHf0sq conxHf0sq site2 site3
xtreg xpostO3 xpreO3 f0 Hf0 libxHf0sq conxHf0sq site2 site3
xtreg xpostO4 xpreO4 f0 Hf0 libxHf0sq conxHf0sq site2 site3
xtreg xpostO5 xpreO5 f0 Hf0 libxHf0sq conxHf0sq site2 site3


* "true" ordinal case
xtologit postO1 preO1 theta0 Htheta0 libxHtheta0sq conxHtheta0sq site2 site3
* measured ordinal case
xtologit postO1 preO1 f0 Hf0 libxHf0sq conxHf0sq site2 site3
* notice that measurement error in the "measured" cases attenuates the estimated parameters, and that the scale change to the ordinal case inflates parameters about 50 percent


saveold "C:\Users\Kevin\Dropbox\UCR\research\bayes\funglee\persuasion\OBOE\Replication\ResultsInPaper\Tutorial\tutorial_data2.dta", replace version(12)



